/**
 * Created by lwy on 2017/8/2.
 */


var page = 1;

function doAjax(){
    $.ajax({
        url:'/crm/roleC/getNormalRole.json',
        dataType:'json',
        data:{
            token:localStorage.getItem('crmtoken'),
            page:page,
        },
        type:'post',
        success: function (data) {
            if(data.code==-1){
                window.parent.location.href="/crm/login.jsp"
            }
            if(data.code==0){
                alert(data.msg);
            }
            if(data.code==1){
                $('#record').html(data.recordsTotal);
                $('#withdraw-record').empty();
                for(var i=0;i<data.datas.length;i++){
                    $('<tr class="gradeA odd">'+
                        '<a href=""><td class="sorting_1 withdrawId" style="text-decoration: underline;cursor: pointer;" id="'+data.datas[i].id+'">'+data.datas[i].id+'</td></a>'+
                        '<td class="sorting_1">'+data.datas[i].name+'</td>'+
                        '<td class="center ">'+data.datas[i].label+'</td>'+
                        '<td class=" ">'+data.datas[i].isValid+'</td>'+
                        '<td class=" ">'+data.datas[i].isInit+'</td>'+
                        '</tr>').appendTo($('#withdraw-record'));
                }

                //用户权限树
                $('.withdrawId').click(function(){
                    id = $(this).attr('id');
                    localStorage.removeItem('crmUserId');
                    localStorage.removeItem('crmTree');
                    localStorage.setItem('crmRoleId',id);
                    window.open('/crm/userRolePrivilegeC/tree')

                })

                $(".pagination").createPage({
                    totalPage:data.totalPage,
                    currPage:data.pageNow,
                    pageCount:data.pageNow,
                    turndown:'true',
                    backFn:function(p){
                        page=p;
                        doAjax();
                    }
                });
            }
        }
    })
}

$(function(){
    doAjax();
})

//查询
$('#refer').click(function(){
    userAccount = $('#accountName').val();
    realName = $('#name').val();
    userStateValue = $('#userState').val();
    page=1;
    doAjax();
})



//点击保存
$('#save').click(function(){

    $.ajax({
        url:'/crm/roleC/addRole.json',
        dataType:'json',
        data:{
            token:localStorage.getItem('crmtoken'),
            name:$('#userAccount').val(),
            label:$('#userName').val(),
            isInit:$('#chooselevel').val(),
        },
        type:'post',
        success:function(data){
            if(data.code==0){
                alert(data.msg)
                return;
            }
            if(data.code==-1){
                window.parent.location.href="/crm/login.jsp"
            }
            if(data.code==1){
                alert('创建成功');
                $('#myModal5').removeClass('fade in');
                $('#myModal5').addClass('fade');
                $('#myModal5').attr('aria-hidden',true);
                $('#myModal5').css('display','none');
                $('.modal-backdrop').css('display','none');
                $('#myModal5 input').val('');
                page=1;
                doAjax();
            }
        }
    })
})

